package com.zixuan.gulicollege.eduservice.controller;


import com.zixuan.gulicollege.common_utils.R;
import com.zixuan.gulicollege.eduservice.service.EduChapterService;
import com.zixuan.gulicollege.entity.eduservice.EduChapter;
import com.zixuan.gulicollege.entity.eduservice.vo.ChapterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zixuan
 * @since 2021-05-08
 */
@RestController
@RequestMapping("/edu_service/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    /**
     * 课程大纲列表，通过课程id进行查询
     * @param courseId
     * @return
     */
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVO> list = chapterService.getChapterVideoByCourseId(courseId);

        return R.ok().data("allChapterVideo", list);
    }

    /**
     * 添加课程分类
     * @param chapter
     * @return
     */
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter chapter) {
        chapterService.save(chapter);
        return R.ok();
    }

    /**
     * 通过id获取课程章节
     * @param chapterId
     * @return
     */
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", eduChapter);
    }

    /**
     * 通过id修改课程章节
     * @param chapter
     * @return
     */
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter chapter) {
        chapterService.updateById(chapter);
        return R.ok();
    }

    /**
     * 通过id删除课程章节
     * @param chapterId
     * @return
     */
    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean flag = chapterService.deleteChapter(chapterId);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


}

